package com.base.myproject.client.busi.bcode;

import java.util.Arrays;

import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseEditorTreeGrid;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComBoxFromBinding;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.codenamemap.CodeNameMapFactory;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid.ClicksToEdit;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;
/**
 * 未做完
 * 有bug，checkbox在初始化的时候调用了set，给basetreemodeldata设置了busistatus，于是trysave的时候，多执行了一句sql
 * @author Administrator
 *2010-3-31
 * javazgw@gmail.com
 */
public class BcodeTreePanel extends BaseJDBCPanel {
	BaseFormPanel bfp;
	Button addbutton ;
	Button delbutton ;
	Button savebutton;
	TextField<String> ccode_t ;
	TextField<String> bname_t ;
	BaseComboBoxForm<BaseModelData> sex_t ;
	TextField<String> edu_t ;
	DateField bird_t ;
	DateField incom_t ;
	TextField<String> mobil_t ;
	TextField<String> idno_t ;
	TextField<String> liveaddr_t ;
	
	
	TextField<String>  shortcode_t;
	BaseComboBoxForm<BaseModelData> groupicode_t;
	
	BaseComboBoxForm<BaseModelData> systemid_t;
	CheckBox isvirtual_c;
	public BcodeTreePanel() {
		super();
		init();
	}

	public void init() {
		busi = SystemMessage.busi[9];

		// this.setAutoHeight(true);
		this.setAutoHeight(true);
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWeiget(), centerData);

	

		this.setBusino(busi);
		setdefault();

	}
	

	private void setdefault() {
		
		
	}

	public void complete() {
		super.complete();
	
		getToolbarll().clear();
//		Button addbutton ;
//		Button delbutton ;
//		Button savebutton;
//		getToolbarll().add(Add);
//		Add.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
//				.addrecord()));
//		getToolbarll().add(refash);
//		refash.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
//				.fresh()));
		
		addbutton.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.addrecord()));
		delbutton.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.deleterecord()));
		savebutton.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
						.save2_16()));
		getToolbarll().add(addbutton);
		getToolbarll().add(delbutton);
		getToolbarll().add(savebutton);
	}

	public ContentPanel initWeiget() {
		
		 addbutton = new Button("添加");
		 delbutton = new Button("删除");
		 savebutton = new Button("保存");
		 
		 
			addbutton.addListener(Events.Select, getListener());
			delbutton.addListener(Events.Select, getListener());
			savebutton.addListener(Events.Select, getListener());
			
			
		createformb();
		ContentPanel panel = new ContentPanel();
		ContentPanel grid_panel = new ContentPanel();
		grid_panel.setHeaderVisible(false);
		//grid_panel.setHeight(400);
		grid_panel.add(createGrid());
//		//grid_panel.setLayout(new FitLayout());
//		//grid_panel.setScrollMode(Scroll.AUTOY);
//		panel.setHeaderVisible(false);
//		panel.setHeight("200");
//		panel.setScrollMode(Scroll.AUTOY);
//		panel.setLayout(new FitLayout());
//		panel.add(grid_panel);
//		panel.add(formpanel);
		
		VerticalPanel v = new VerticalPanel();
		v.add(grid_panel);
		v.add(formpanel);
		
		//panel.setHeight("200");
		panel.setScrollMode(Scroll.AUTOY);
		panel.setLayout(new FitLayout());
		panel.add(v);
		
		return panel;
	}

	BaseTreeStore<TreeNodeModelData> store = null;

	BaseTreeStore<TreeNodeModelData> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseTreeStore<TreeNodeModelData> createStore() {
		final StoreInfo si = new StoreInfo();
		si
				.setSql("select icode,bcode,bname,sex,edu,bird,incom,mobil,idno,liveaddr,bcodeshortcode,groupicode,systemid,isvirtual from bcode where bcode is not null order by bcode");
		si.setTreesortcolumn("bcode");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("bcode");
		

		store = new BaseTreeStore<TreeNodeModelData>(si, false);
		store.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED, new BaseStoreListener(){

			@Override
			public void BaseStoreEventHandle(BaseStoreEvent evt) {
			
				showvisial(evt.getBasetreestore().getValue("isvirtual").equals("true")||evt.getBasetreestore().getValue("isvirtual").equals(true));			
			}

			@Override
			public void onBrowserEvent(Event event) {
				// TODO Auto-generated method stub
				
			}
			
		});
		
		return store;
	}

	 BaseEditorTreeGrid<ModelData>  grid = null;

	 BaseEditorTreeGrid<ModelData> createGrid() {
		ColumnConfig name = new ColumnConfig("bcode", "人员代码", 100);
		name.setRenderer(new TreeGridCellRenderer<ModelData>());
		TextField<String> text = new TextField<String>();
		text.setAllowBlank(false);
		name.setEditor(new CellEditor(text));

		ColumnConfig bname = new ColumnConfig("bname", "名称", 100);
		bname.setEditor(new CellEditor(new TextField<String>()));
		
		
		ColumnConfig systemid = new ColumnConfig("systemid", "公司代码", 100);
		ColumnModel cm = new ColumnModel(Arrays.asList(name,  bname,systemid));
	
		
		final BaseEditorTreeGrid<ModelData> grid = new BaseEditorTreeGrid<ModelData>(
				getStore(), cm);
		grid.setClicksToEdit(ClicksToEdit.TWO);
		grid.setBorders(true);
		
		//grid.setSize(400, 200);
		grid.setHeight(200);
		grid.setAutoWidth(true);
		grid.setAutoExpandColumn("bname");
		grid.setTrackMouseOver(false);
//		
		return grid;
	}
	 

	 final FormPanel formpanel = new FormPanel();
	 public void createformb() {
		 
		// final FormPanel formpanel = new FormPanel();
		 //------------------------------
		 formpanel.setLabelWidth(100);
		 formpanel.setFieldWidth(100);
			Label ccode_l = new Label("编号:");
			Label isvirtual_l = new Label("虚拟目录");
			
			//ccode_l.setWidth("20");
			Label sex_l = new Label("性别:");
		//	sex_l.setWidth("40");
			Label bname_l = new Label("名字:");
			//bname_l.setWidth("40");

			Label edu_l = new Label("学历:");
			Label bird_l = new Label("出生日期:");
			Label incom_l = new Label("入司日期:");
			Label mobil_l = new Label("手机号:");
			Label idno_l = new Label("身份证号:");
			Label liveaddr_l = new Label("居住地址:");
			Label shortcode_l = new Label("简码:");
			Label groupicode_l = new Label("用户所在组:");
			Label systemid_l = new Label("所在公司/店面:");
			
			  isvirtual_c = new CheckBox();  
			 isvirtual_c.setBoxLabel("是否是虚拟目录");  
			 isvirtual_c.setName("isvirtual");
			 isvirtual_c.setToolTip("选中表示是一个公司、店面、部门的名称，否则是人员。");
			 isvirtual_c.addListener(Events.Change, getListener());
			
			 ccode_t = new TextField<String>();
			 bname_t = new TextField<String>();
			// sex_t = new TextField<String>();
			 sex_t = CodeNameMapFactory.createSexInput_F();
				
			 edu_t = new TextField<String>();
			 bird_t = new DateField();
			 bird_t.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));  
			 incom_t = new DateField();
			 incom_t.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss"));  
			 mobil_t = new TextField<String>();
			 idno_t = new TextField<String>();
			 liveaddr_t = new TextField<String>();
			 shortcode_t = new TextField<String>();
			 groupicode_t = com.base.myproject.client.ui.codenamemap.CodeNameMapFactory.createGroupInput_F();
			 //groupicode_t.addListener(Events.Change, this);
			 groupicode_t.setAllowBlank(false);
			 
			 systemid_t =  com.base.myproject.client.ui.codenamemap.CodeNameMapFactory.createSysteminfoInput_F();
			 systemid_t.setAllowBlank(false);
			 
			 
			 isvirtual_c.setName("isvirtual") ;
			 ccode_t.setName("bcode") ;
			 bname_t.setName("bname") ;
			 sex_t.setName("sex");
			 edu_t.setName("edu") ;
			 bird_t.setName("bird") ;
			 incom_t.setName("incom") ;
			 mobil_t.setName("mobil") ;
			 idno_t.setName("idno") ;
			 liveaddr_t.setName("liveaddr") ;
			 shortcode_t.setName("bcodeshortcode") ;
			 groupicode_t.setName("groupicode");
			 systemid_t.setName("systemid");
			 
			//this.setHeaderVisible(false);
			this.setHeight("200");
			TableLayout tl = new TableLayout(4);
			tl.setWidth("600");
			tl.setCellSpacing(10);
			
			formpanel.setLayout(tl);
			formpanel.setFieldWidth(200);
			
			TableData td=new TableData();
//			td.setMargin(20);
//			td.setPadding(20);
			td.setColspan(1);
		//	td.setWidth("600");
			td.setMargin(100);
			td.setPadding(5);
			td.setRowspan(1);
			td.setWidth("200");
			
			formpanel.add(isvirtual_l,td);
			formpanel.add(isvirtual_c,td);
			formpanel.add(systemid_l,td);
			formpanel.add(systemid_t,td);
			formpanel.add(ccode_l,td);
			formpanel.add(ccode_t,td);
			formpanel.add(bname_l,td);
			formpanel.add(bname_t,td);
			formpanel.add(shortcode_l,td);
			formpanel.add(shortcode_t,td);
			
			formpanel.add(sex_l,td);
			formpanel.add(sex_t,td);
			formpanel.add(edu_l,td);
			formpanel.add(edu_t,td);
			formpanel.add(bird_l,td);
			formpanel.add(bird_t,td);
			formpanel.add(incom_l,td);
			formpanel.add(incom_t,td);
			formpanel.add(mobil_l,td);
			formpanel.add(mobil_t,td);
			formpanel.add(idno_l,td);
			formpanel.add(idno_t,td);
			formpanel.add(liveaddr_l,td);
			formpanel.add(liveaddr_t,td);
			 
			formpanel.add(groupicode_l,td);
			formpanel.add(groupicode_t,td);
			
			
		
//			 CheckBoxGroup checkGroup = new CheckBoxGroup();  
//			 checkGroup.setFieldLabel("Music");  
			 CheckBox check1 = new CheckBox();  
			 check1.setBoxLabel("允许给会员充值");  
			 check1.setName("canaddfinance");
			 check1.setToolTip("选择后可以有给会员充值的权限");
			 
			 CheckBox check2 = new CheckBox();  
			 check2.setBoxLabel("允许下载数据");  
			 check2.setName("candownload");
			 check2.setToolTip("选择后可以下载资料");
			 
			 CheckBox check3 = new CheckBox();  
			 check3.setBoxLabel("可以改会员信息");  
			 check3.setName("caneditccode");
			 check3.setToolTip("选择后有旧的会员信息进行修改的权限");
			 
			 
			 CheckBox check4 = new CheckBox();  
			 check4.setBoxLabel("可以改收单管理数据");  
			 check4.setName("canchangepo");
			 check4.setToolTip("选择后有对已经在“收单管理”里已经“收单”的数据进行修改的权限");
			// check2.setValue(true);  
//			 checkGroup.add(check2);
			 formpanel.add(check1,td);
			 formpanel.add(check2,td);
			 formpanel.add(check3,td);
			 formpanel.add(check4,td);
		
				
			
//			HorizontalPanel hpanel = new HorizontalPanel();
//			
//			
//			hpanel.add(addbutton);
//			hpanel.add(delbutton);
//			hpanel.add(savebutton);
//
//			formpanel.add(hpanel,td);
//		 
		 
		 
		 //------------------------------
		 
		final FormBinding formbinding = new FormBinding(formpanel, true){
				  public void autoBind() {
					    for (Field<?> f : panel.getFields()) {
					      if (!bindings.containsKey(f.getId())) {
					        String name = f.getName();
					        if (name != null && name.length() > 0) {
					        	if(f instanceof BaseComboBoxForm<?>)
					        	{
					        		BaseComBoxFromBinding bcbfb = new BaseComBoxFromBinding((BaseComboBoxForm)f,f.getName());
					        		bindings.put(f.getId(), bcbfb);
					        	}
					        	else
					        	{
					        		FieldBinding b = new FieldBinding(f, f.getName());
					        		bindings.put(f.getId(), b);
					        	}
					        }
					      }
					    }
					  }

			};
			formbinding.setStore(getStore());
//			formbinding.addListener(Events.Bind, this);
//			formbinding.addListener(Events.UnBind, this);
//			formbinding.addListener(Events.BeforeBind, this);
			getStore().addBaseStoreListener(BaseStoreEvent.EventType.LOAD_STARTED,
					new BaseStoreListener() {
						@Override
						public void BaseStoreEventHandle(BaseStoreEvent evt) {

						}

						@Override
						public void onBrowserEvent(Event event) {
							// TODO Auto-generated method stub

						}

					});
			getStore().addBaseStoreListener(BaseStoreEvent.EventType.LOAD_COMPLETE,
					new BaseStoreListener() {

						@Override
						public void BaseStoreEventHandle(BaseStoreEvent evt) {

							formpanel.mask("loading");
							if (getStore().getCount() > 0)
								{
								//formbinding.autoBind();
								formbinding.bind(getStore().gotorow(0));
								}
							else {
								formbinding.unbind();
							}
							// formpanel.el().mask().hide();
							formpanel.unmask();

						}

						@Override
						public void onBrowserEvent(Event event) {
							// TODO Auto-generated method stub

						}

					});

			getStore().addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED,
					new BaseStoreListener() {

						@Override
						public void BaseStoreEventHandle(BaseStoreEvent evt) {
							formbinding.bind(getStore().getAt(evt.getCurrowindex()));

						}

						@Override
						public void onBrowserEvent(Event event) {
							// TODO Auto-generated method stub

						}

					});
		}

	 int sync = 0;
		Listener<BaseEvent> defaultlistener;

		Listener<BaseEvent> getListener() {
			if (defaultlistener != null) {
				return defaultlistener;
			}

			defaultlistener = new Listener<BaseEvent>() {
				@SuppressWarnings("unchecked")
				public void handleEvent(BaseEvent be) {
					sync++;
					try {
						if (sync == 1) {
							if (be.getType() == Events.Select) {
								if (be.getSource().equals(addbutton)) {
								//	getStore().add(item, addChildren)
									getStore().add();
								}
								else 	if (be.getSource().equals(delbutton)) {
									getStore().remove();
								}
								else 	if (be.getSource().equals(savebutton)) {
									getStore().trySave();
								}

							} else if (be.getType() == Events.Hide) {

							} else if (be.getType() == Events.Change) {
								if (be.getSource().equals(isvirtual_c)) {

									showvisial(isvirtual_c.getValue());
								}
							} else if (be.getType() == Events.KeyUp) {
								if (be instanceof ComponentEvent) {
									if (((ComponentEvent) be).getKeyCode() == 13) // 回车
									{
									}
								}
							}
							else if (be.getType() == Events.SelectionChange) {
								
							}
							
						}
					} finally {
						sync--;
					}
				}

			};
			return defaultlistener;
		}
		
		private void showvisial(boolean b)
		{
		
			
			for(int i=8;i<30;i++)
			{
			formpanel.getWidget(i).setVisible(!b);
			}
		}
}
